package leetcode_interview;

public class I0105 {

    public boolean oneEditAway1(String first, String second) {
        int firstLength = first.length();
        int secondLength = second.length();
        if (Math.abs(firstLength - secondLength) >= 1)
            return false;
        if (firstLength == secondLength) {
            if (first.equals(second))
                return true;
            int differentCount = 0;
            for (int i = 0; i < firstLength; i++) {
                if (differentCount >= 1)
                    return false;
                if (first.charAt(i) != second.charAt(i))
                    differentCount++;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        I0105 i0105=new I0105();
        System.out.println(i0105.oneEditAway("abc","ac"));
    }

    public boolean oneEditAway(String first, String second) {
        int firstLength = first.length();
        int secondLength = second.length();
        if (Math.abs(firstLength - secondLength) > 1)
            return false;
        int i = 0;
        while (i < firstLength && i < secondLength && first.charAt(i) == second.charAt(i)) {
            i++;
        }
        int j = firstLength - 1, k = secondLength - 1;
        while (j >= 0 && k >= 0 && first.charAt(j) == second.charAt(k)) {
            k--;
            j--;
        }
        return j - i < 1 && k - i < 1;
    }
}
